home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / pthd-0.000 / pthd-0 / pthd-0.7 / src_thread / thread.c.1 < prev    next >
Encoding:
Text File  |  1995-08-16  |  3.6 KB  |  151 lines

  1. /*
  2.  * --  thread.c.1
  3.  *     Tests the thread-specific data API.
  4.  */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <pthread.h>
  8. #include "utils.h"
  9.  
  10. #define ITERATIONS ((int)1000000)
  11. #define NUM_THREADS ((int)1)
  12. #define MAX_NAME_SIZE ((size_t) 24)
  13.  
  14. static void
  15. destroy_data( void *data );
  16.  
  17. typedef struct FULL_NAME
  18. {
  19.    char first[MAX_NAME_SIZE];
  20.    char last[MAX_NAME_SIZE];
  21. } full_name_t;
  22.  
  23. static pthread_t *th;
  24. static pthread_attr_t attr_h;
  25. static pthread_key_t name_key, address_key;
  26. static char *address = "40 Elm St.";
  27.  
  28. void
  29. thread_proc( full_name_t *thread_name )
  30. {
  31.    long sum = 0;
  32.    int st, i;
  33.    full_name_t *name = NULL;
  34.    char *addr;
  35.  
  36.    for(i = 0; i < ITERATIONS; i++ )
  37.        sum += 1;
  38.  
  39.    /*
  40.     *  --  Set the name and address of the thread.
  41.     */
  42.    st = pthread_setspecific( name_key, thread_name );
  43.    CHECK(st, "pthread_setspecific()");
  44.    st = pthread_setspecific( address_key, address );
  45.    CHECK(st, "pthread_setspecific()");
  46.  
  47.    for(i = 0; i < ITERATIONS * 2; i++ )
  48.        sum += 1;
  49.  
  50.    st = pthread_getspecific( name_key, (void **) &name );
  51.    CHECK(st, "pthread_getspecific()");
  52.    st = pthread_getspecific( address_key, (void **) &addr );
  53.    CHECK(st, "pthread_getspecific()");
  54.  
  55.    for(i = 0; i < ITERATIONS; i++ )
  56.        sum += 1;
  57.  
  58.    printf_r("%s %s\n", name->first, name->last );
  59.    printf_r("%s\n", addr );
  60.  
  61.    pthread_exit( (void *) SUCCESS );
  62. }
  63.  
  64. static pthread_once_t key_once = PTHREAD_ONCE_INIT;
  65.  
  66. static void 
  67. init_key( void )
  68. {
  69.    int st;
  70.  
  71.    st = pthread_key_create( &name_key, destroy_data );
  72.    CHECK(st, "pthread_key_create()");
  73.  
  74.    st = pthread_key_create( &address_key, NULL );
  75.    CHECK(st, "pthread_key_create()");
  76. }
  77.  
  78. int 
  79. main( int argc, char *argv[] )
  80. {
  81.    int i, st, exit_val = 0, thread_count = NUM_THREADS;
  82.    full_name_t *full_name;
  83.  
  84.    if( argc == 2 )
  85.        thread_count = atoi( argv[1] );
  86.  
  87.    st = pthread_once( &key_once, init_key ); 
  88.    CHECK(st, "pthread_once()");
  89.  
  90.    /*
  91.     *  --  Since threads are created, by default, as detached, then
  92.     *      we must create them with an attributes object initialized
  93.     *      with the PTHREAD_CREATE_JOINABLE attribute.
  94.     */
  95.    st = pthread_attr_init( &attr_h );
  96.    CHECK(st, "pthread_attr_init()");
  97.    st = pthread_attr_setdetachstate( &attr_h, PTHREAD_CREATE_JOINABLE );
  98.    CHECK(st, "pthread_attr_setdetachstate()");
  99.  
  100.    /*
  101.     *  --  Create and join a bunch of threads.
  102.     */
  103.    th = malloc_r( thread_count * sizeof( pthread_t ));
  104.    for(i = 0; i < thread_count; i++ )
  105.    {
  106.        full_name = malloc_r( sizeof( full_name_t ));
  107.        pthread_lock_global_np();
  108.        sprintf( full_name->first, "%s_%d", "bob", i );
  109.        sprintf( full_name->last, "%s_%d", "smith", i );
  110.        pthread_unlock_global_np();
  111.  
  112.        st = pthread_create( &th[i], 
  113.                             &attr_h, 
  114.                             (thread_proc_t) thread_proc,
  115.                             (void *) full_name );
  116.  
  117.        CHECK(st, "pthread_create()");
  118.    }
  119.  
  120.    /*
  121.     *  --  Join with each one.  The pthread_join() function automatically
  122.     *      detaches the thread once the join is complete.  This means that
  123.     *      a thread may be joined only once.
  124.     */
  125.    for(i = 0; i < thread_count; i++ )
  126.    {
  127.        st = pthread_join( th[i], (void **) &exit_val );
  128.        CHECK(st, "pthread_join()");
  129.  
  130.        if( exit_val != SUCCESS )
  131.            fprintf_r( stderr, "Failed to join with th[%d]!\n", i);
  132.    }
  133.  
  134.    st = pthread_attr_destroy( &attr_h );
  135.    CHECK(st, "pthread_attr_destry()");
  136.  
  137.    free_r( th );
  138.  
  139.    for(i = 0; i < ITERATIONS; i++ )
  140.        ; 
  141.  
  142.    print_system_counters();
  143.    return( EXIT_SUCCESS );
  144. }
  145.  
  146. static void
  147. destroy_data( void *data )
  148. {
  149.    free_r( data );
  150. }
  151.